<?php

namespace App\Admin\Controllers\Act;

use App\Admin\Extensions\VoiceCallOp;
use App\Http\Controllers\Controller;
use App\Model\Message;
use App\Model\SigninLog;
use App\Model\UserProfile;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class SigninController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户签到记录');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SigninLog());
        $grid->model()->orderBy('id', 'desc');
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->avatar('头像')->display(function ($data) {
            if (!$data) return '';
            return ComUtil::buidlImgaeHtml($data);
        });
        $grid->userProfile()->nick_name('拨打方昵称');
        $grid->userProfile()->role('用户角色')->display(function ($role) {
            return $role == UserProfile::ROLE_HOST ? '女用户' : '普通用户';
        });
        $grid->signin_day('连续签到的天数');
        $grid->created_at('签到时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s',  strtotime($time));
        });
        $grid->userProfile()->created_at('注册时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s',  strtotime($time));
        });
        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->between('created_at', '签到时间')->datetime();
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }

    /**
     * 话单操作
     */
    function call_op()
    {
        $id = request('id');
        $callType = request('extra');
        $callCls = $callType == VoiceCall::CALL_TYPE ? VoiceCall::class : VideoCall::class;
        $record = $callCls::where('id', $id)->first();
        if (!$record) return ['ret' => -1, 'message' => "找不到该话单记录！"];
        //修改状态
        $ts = time();
        $info = [
            'status' => $callCls::STATUS_ADMIN_HANGUP,
            'end_time' => $ts,
        ];
        if ($record->answer_time > 0) $info['call_time'] = $ts - $record->answer_time;
        $record->update($info);
        //暂停通话
        Message::sendSlient($record->user_id, 'HANGUP_CALL', ['tip' => '本次通话已结束～']);
        Message::sendSlient($record->chat_user_id, 'HANGUP_CALL', ['tip' => '本次通话已结束～']);
        //由声网回调自己结算
        return ['ret' => 0, 'message' => "成功关闭话单！ID：{$id}"];
    }
}
